package com.woniuxy.carrental.controller;

import com.woniuxy.carrental.entity.Emp;
import com.woniuxy.carrental.entity.PageBean;
import com.woniuxy.carrental.entity.ResponseResult;
import com.woniuxy.carrental.service.EmpService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * controller调用
 */
//@Controller
@RestController
@RequestMapping("/admin")
public class EmpController {

    @Autowired
    private EmpService empService;



    @GetMapping("/login")
    public ResponseResult doLogin1(String userName, String password, Model model,
    HttpSession session){
        System.out.println(session.getId());
        //认证就是登录，输入账号密码
        UsernamePasswordToken token=new UsernamePasswordToken(userName, password);
        System.out.println(token.getUsername());
        //创建Subject,就是当前用户
        Subject subject = SecurityUtils.getSubject();
        try{
            //登录认证
            subject.login(token);
            //记录Session
//            Emp emp = (Emp) subject.getSession().getAttribute("emp");
            return new ResponseResult(token.getUsername(),200,"登陆成功");
        }catch (UnknownAccountException ex){
            return  new ResponseResult(null,401,"用户名不存在");
        }catch (IncorrectCredentialsException ex){
            return  new ResponseResult(null,401,"密码错误");
        }catch (AuthenticationException ex) {
            return new ResponseResult(null, 401, "登录错误");
        }
    }


    @RequiresPermissions("select")
    //查询员工
    @GetMapping("/getEmps")
    public ResponseResult<PageBean<Emp>> getEmps(Emp emp,String empname, String status,int pageSize, int pageIndex) {
        List<Emp> emps = empService.findAllEmpsByPage(emp, empname,status,pageIndex, pageSize);
        for(Emp emp1 : emps){
            System.out.println(33);
            System.out.println(emp1);
        }
        PageBean<Emp> pageBean=new PageBean<>();
        pageBean.setTotalRecord(empService.findEmpsCountByPage(emp));
        pageBean.setBeanList(emps);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        return new ResponseResult<>(pageBean,200,"ok");
    }

    @RequiresPermissions("save")
    //添加员工
    @PostMapping("/addEmps")
    public String addEmp(@RequestBody Emp emp) {
        if(emp.getEmpname()==null||emp.getEmpname().trim().equals("")) {
            return "名称不能为空";
        }
        if(emp.getAccount().trim().length()<2||emp.getAccount().trim().length()>6) {
            return "账户长度3到6";
        }
        empService.addEmp(emp);
        return "添加成功";
    }
    //删除员工
    @RequiresPermissions("delete")
    @DeleteMapping("/deleteEmp/{account}")
    public String deleteEmp(@PathVariable String account) {
        System.out.println(account);
        empService.deleteEmp(account);
        return "删除成功";
    }

    //修改员工信息
    @RequiresPermissions("update")
    @PutMapping("/updateEmp")
    public String updateEmp(@RequestBody Emp emp) {
        System.out.println(emp);
        empService.updateEmp(emp);
        return "修改成功";

    }
    @ExceptionHandler
    public  ResponseResult exceptionHandler(AuthorizationException ex) {
        System.out.println(ex.getMessage());
        return new ResponseResult(null,402,"没有授权");
    }

}